package cn.wolfcode.web.controller;
import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageResult",pageInfo);
        return "department/list";
    }
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(Long id){

            if(id != null){
                departmentService.deleteByPrimaryKey(id);
            }
            return new JsonResult();
    }
    /*@RequestMapping("/input")
    public String input(Long id,Model model){
        if(id != null){
            Department department = departmentService.selectByPrimaryKey(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }*/
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department){
            if(department.getId()!=null){
                departmentService.updateByPrimaryKey(department);
            }else{
                departmentService.insert(department);
            }
            return new JsonResult();
    }

}
